package com.aaa.system.service.impl;

import com.aaa.entity.Menu;
import com.aaa.security.util.WebUtils;
import com.aaa.system.dao.MenuDao;
import com.aaa.system.service.MenuService;
import com.aaa.system.service.RoleService;
import com.aaa.util.JwtUtil;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author 崔灿
 * @Date 2023/02/27 09:22
 * @ClassName MenuServiceImpl
 * @Description
 * @Version 1.0
 */

@Service
public class MenuServiceImpl extends ServiceImpl<MenuDao, Menu> implements MenuService {
    @Autowired
    private MenuDao menuDao;

    @Override
    public List<Menu> selectPermsByUserid(Integer userid) {
        return menuDao.selectPermissionByUserid(userid);

    }

    /**
     * 获取菜单
     * @param menus
     * @return
     */
    public static List<Menu> menuUtil(List<Menu> menus){
        //1.一级菜单
        List<Menu> firsts = new ArrayList<>();
        for (Menu menu : menus) {
            if (menu.getParentId()==0){
                firsts.add(menu);
            }
        }
        System.out.println("firsts = " + firsts);
        //2.一级菜单下的二级菜单
        for (Menu first : firsts) {
            List<Menu> seconds = new ArrayList<>();
            for (Menu menu : menus) {
                if (menu.getParentId().equals(first.getMenuId())){
                    seconds.add(menu);
                }
            }
            first.setChildren(seconds);
            for (Menu second:seconds){
                ArrayList<Menu> thirds = new ArrayList<>();
                for (Menu menu1 : menus){
                    if (menu1.getParentId().equals(second.getMenuId())){
                        thirds.add(menu1);
                    }
                }
                second.setChildren(thirds);
            }
        }
        return firsts;
    }




    @Override
    public Result findAllMenu() {
        String token = WebUtils.getRequest().getHeader("token");
        System.out.println(token);
        Map<String, Object> tokenChaim = JwtUtil.getTokenChaim(token);
        System.out.println(tokenChaim);
        String username = (String) tokenChaim.get("username");
        Integer userid = menuDao.findUserid(username);
        List<Menu> allMenu = menuDao.findAllMenu(userid);
        System.out.println("allMenu = " + allMenu);
        List<Menu> menus = menuUtil(allMenu);
        System.out.println("menus = " + menus);
        return new Result(2000,"成功",menus);

    }

    @Override
    public Result addMenu(Menu menu) {
        int insert = menuDao.insert(menu);
        return new Result(2000,"成功",insert);
    }

    @Override
    public Result deleteMenu(Integer menuId) {
        int i = menuDao.deleteById(menuId);
        return new Result(2000,"成功",i);
    }

    @Override
    public Result selectAll() {
        List<Menu> menus = menuDao.selectAll();
        List<Menu> menus1 = menuUtil(menus);
        return new Result(2000,"成功",menus1);
    }

    @Override
    public Result updateMenu(Menu menu) {
        int i = menuDao.updateById(menu);
        return new Result(2000,"成功",i);
    }



}
